cmake_minimum_required(VERSION 2.4)
include(CheckIncludeFiles)
include(CheckFunctionExists)
include(CheckLibraryExists)

macro(optional variable header library function source)
  check_include_files(${header} ${variable}1)
  if (${variable}1)
    check_library_exists(${library} ${function} "" ${variable})
    if (${variable})
      set(optional_srcs ${optional_srcs} ${source})
      set(optional_libs ${optional_libs} ${library})
    endif (${variable})
  endif (${variable}1)
endmacro(optional)

macro(optional2 variable header library1 function1 library2 function2 source)
  check_include_files(${header} ${variable}1)
  if (${variable}1)
    check_library_exists(${library1} ${function1} "" ${variable}2)
    if (${variable}2)
      check_library_exists(${library2} ${function2} "" ${variable})
      if (${variable})
	set(optional_srcs ${optional_srcs} ${source})
	set(optional_libs ${optional_libs} ${library1} ${library2})
      endif (${variable})
    endif (${variable}2)
  endif (${variable}1)
endmacro(optional2)

macro(optional3 variable header library1 function1 library2 function2 library3 function3 source)
  check_include_files(${header} ${variable}1)
  if (${variable}1)
    check_library_exists(${library1} ${function1} "" ${variable}2)
    if (${variable}2)
      check_library_exists(${library2} ${function2} "" ${variable}3)
      if (${variable}3)
	check_library_exists(${library3} ${function3} "" ${variable})
        if (${variable})
          set(optional_srcs ${optional_srcs} ${source})
          set(optional_libs ${optional_libs} ${library1} ${library2} ${library3})
        endif (${variable})
      endif (${variable}3)
    endif (${variable}2)
  endif (${variable}1)
endmacro(optional3)

macro(optional4 variable header library1 function1 library2 function2 library3 function3 library4 function4 source)
  check_include_files(${header} ${variable}1)
  if (${variable}1)
    check_library_exists(${library1} ${function1} "" ${variable}2)
    if (${variable}2)
      check_library_exists(${library2} ${function2} "" ${variable}3)
      if (${variable}3)
	check_library_exists(${library3} ${function3} "" ${variable}4)
	if (${variable}4)
	  check_library_exists(${library4} ${function4} "" ${variable})
	  if (${variable})
	    set(optional_srcs ${optional_srcs} ${source})
	    set(optional_libs ${optional_libs} ${library1} ${library2} ${library3} ${library4})
	  endif (${variable})
	endif (${variable}4)
      endif (${variable}3)
    endif (${variable}2)
  endif (${variable}1)
endmacro(optional4)

project(sox C)

if(CMAKE_COMPILER_IS_GNUCC)
	add_definitions(-fstack-protector -Wall -W -Wmissing-prototypes -Wstrict-prototypes -pedantic -Wno-format -Wno-long-long)
endif(CMAKE_COMPILER_IS_GNUCC)

include(TestBigEndian)

check_include_files("byteswap.h"         HAVE_BYTESWAP_H)
check_include_files("inttypes.h"         HAVE_INTTYPES_H)
check_include_files("fenv.h"             HAVE_FENV_H)
check_include_files("glob.h"             HAVE_GLOB_H)
check_include_files("io.h"               HAVE_IO_H)
#check_include_files("ltdl.h"             HAVE_LTDL_H) # no plug-ins as yet
check_include_files("stdint.h"           HAVE_STDINT_H)
check_include_files("string.h"           HAVE_STRING_H)
check_include_files("strings.h"          HAVE_STRINGS_H)
check_include_files("sys/stat.h"         HAVE_SYS_STAT_H)
check_include_files("sys/time.h"         HAVE_SYS_TIME_H)
check_include_files("sys/timeb.h"        HAVE_SYS_TIMEB_H)
check_include_files("sys/types.h"        HAVE_SYS_TYPES_H)
check_include_files("sys/utsname.h"      HAVE_SYS_UTSNAME_H)
check_include_files("termios.h"          HAVE_TERMIOS_H)
check_include_files("unistd.h"           HAVE_UNISTD_H)

check_function_exists("fmemopen"         HAVE_FMEMOPEN)
check_function_exists("fseeko"           HAVE_FSEEKO)
check_function_exists("gettimeofday"     HAVE_GETTIMEOFDAY)
check_function_exists("mkstemp"          HAVE_MKSTEMP)
check_function_exists("popen"            HAVE_POPEN)
check_function_exists("strcasecmp"       HAVE_STRCASECMP)
check_function_exists("strrstr"          HAVE_STRRSTR)
check_function_exists("vsnprintf"        HAVE_VSNPRINTF)

test_big_endian(WORDS_BIGENDIAN)

optional(NEED_LIBM math.h m pow "")
if(NEED_LIBM)
  set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} -lm)
  optional(HAVE_LRINT math.h m lrint "")
else(NEED_LIBM)
  check_function_exists("lrint" HAVE_LRINT)
endif(NEED_LIBM)
optional(EXTERNAL_GSM gsm/gsm.h gsm gsm_create "")
optional(EXTERNAL_LPC10 lpc10/lpc10.h lpc10 lpc10_create "")
optional(HAVE_ALSA alsa/asoundlib.h asound snd_pcm_open alsa)
optional(HAVE_AMRNB amrnb/sp_dec.h amrnb Decoder_Interface_init amr-nb)
optional(HAVE_AMRWB amrwb/dec.h amrwb D_IF_init amr-wb)
if(CMAKE_COMPILER_IS_GNUCC)
  optional(HAVE_GOMP omp.h gomp omp_get_thread_num "")
  if(HAVE_GOMP)
    add_definitions(-fopenmp)
  endif(HAVE_GOMP)
endif(CMAKE_COMPILER_IS_GNUCC)
optional(HAVE_ID3TAG id3tag.h id3tag id3_file_open "")
optional(HAVE_SNDIO sndio.h sndio sio_open sndio)
optional(HAVE_AO ao/ao.h ao ao_play ao)
optional(HAVE_FLAC FLAC/all.h FLAC FLAC__stream_encoder_new flac)
optional(HAVE_MAD_H mad.h mad mad_stream_buffer mp3)
optional(HAVE_LAME_LAME_H lame/lame.h mp3lame lame_get_lametag_frame mp3)
if (NOT HAVE_LAME_LAME_H)
  optional(HAVE_LAME_LAME_H lame.h mp3lame lame_get_lametag_frame mp3)
endif (NOT HAVE_LAME_LAME_H)
optional(HAVE_TWOLAME_H twolame.h twolame twolame_init mp3)
optional(HAVE_MAGIC magic.h magic magic_open "")
optional2(HAVE_PNG png.h png png_set_rows z uncompress spectrogram)
if (HAVE_PNG)
  check_library_exists(z uncompress "" spectrogram1)
  if (${spectrogram1})
    set(optional_libs ${optional_libs} z)
  endif (${spectrogram1})
endif (HAVE_PNG)
optional2(HAVE_PULSEAUDIO pulse/simple.h pulse-simple pa_simple_new pulse pa_strerror pulseaudio)
optional(HAVE_SNDFILE sndfile.h sndfile sf_open_virtual sndfile)
optional(HAVE_SNDFILE sndfile.h sndfile sf_open_virtual caf)
optional(HAVE_SNDFILE sndfile.h sndfile sf_open_virtual fap)
optional(HAVE_SNDFILE sndfile.h sndfile sf_open_virtual mat4)
optional(HAVE_SNDFILE sndfile.h sndfile sf_open_virtual mat5)
optional(HAVE_SNDFILE sndfile.h sndfile sf_open_virtual paf)
optional(HAVE_SNDFILE sndfile.h sndfile sf_open_virtual pvf)
optional(HAVE_SNDFILE sndfile.h sndfile sf_open_virtual sd2)
optional(HAVE_SNDFILE sndfile.h sndfile sf_open_virtual w64)
optional(HAVE_SNDFILE sndfile.h sndfile sf_open_virtual xi)
optional(HAVE_SPEEXDSP speex/speex_preprocess.h speexdsp speex_preprocess_run speexdsp)
optional(HAVE_SUN_AUDIOIO_H sun/audioio.h c ioctl sunaudio)
if (NOT HAVE_SUN_AUDIOIO_H)
  optional(HAVE_SYS_AUDIOIO_H sys/audioio.h c ioctl sunaudio)
endif (NOT HAVE_SUN_AUDIOIO_H)
optional(HAVE_SYS_SOUNDCARD_H sys/soundcard.h c ioctl oss)
if (NOT HAVE_SYS_SOUNDCARD_H)
  optional(HAVE_MACHINE_SOUNDCARD_H machine/soundcard.h ossaudio _oss_ioctl oss)
endif (NOT HAVE_SYS_SOUNDCARD_H)
optional(HAVE_WAVEAUDIO mmsystem.h winmm waveInGetDevCapsA waveaudio)
optional4(HAVE_OGG_VORBIS vorbis/codec.h ogg ogg_stream_flush vorbis vorbis_analysis_headerout vorbisfile ov_clear vorbisenc vorbis_encode_init_vbr vorbis)
optional3(HAVE_OPUS opusfile.h ogg ogg_stream_flush opus opus_encoder_create opusfile op_open_callbacks opus)
optional(HAVE_WAVPACK wavpack/wavpack.h wavpack WavpackGetSampleRate wavpack)

if (HAVE_LAME_LAME_H OR HAVE_MAD_H)
  set(HAVE_MP3 1)
endif (HAVE_LAME_LAME_H OR HAVE_MAD_H)

set(CMAKE_REQUIRED_LIBRARIES mp3lame m)
check_function_exists("lame_set_VBR_quality" HAVE_LAME_SET_VBR_QUALITY)

if (HAVE_SUN_AUDIOIO_H OR HAVE_SYS_AUDIOIO_H)
  set(HAVE_SUN_AUDIO 1)
endif (HAVE_SUN_AUDIOIO_H OR HAVE_SYS_AUDIOIO_H)

if (HAVE_SYS_SOUNDCARD_H OR HAVE_MACHINE_SOUNDCARD_H)
  set(HAVE_OSS 1)
endif (HAVE_SYS_SOUNDCARD_H OR HAVE_MACHINE_SOUNDCARD_H)

configure_file(sox.pc.in sox.pc @ONLY)
install_files(/lib/pkgconfig FILES sox.pc)

subdirs(src)

if (NOT EXTERNAL_GSM)
  add_subdirectory(libgsm)
endif (NOT EXTERNAL_GSM)

if (NOT EXTERNAL_LPC10)
  add_subdirectory(lpc10)
endif (NOT EXTERNAL_LPC10)

if(APPLE)
	find_library(COREAUDIO_LIBRARY CoreAudio)
	mark_as_advanced(COREAUDIO_LIBRARY)
	if(COREAUDIO_LIBRARY)
		set(HAVE_COREAUDIO 1)
		set(optional_srcs ${optional_srcs} coreaudio)
	endif(COREAUDIO_LIBRARY)
	set(optional_libs ${optional_libs} ${COREAUDIO_LIBRARY})
endif(APPLE)


